package com.lin.cloud.disk.common.aop;

import com.alibaba.fastjson.JSON;
import com.lin.cloud.disk.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 控制层AOP切面增强
 * @author ljc
 * @date 2020/12/10 13:54
 */
@Slf4j
@Aspect
@Component
public class ControllerAOP {

    /**
     * controller层切点
     */
//    @Pointcut("execution(* com.lin.blog2020.modules.*.controller.*.*(..))")
//    public void pointcut(){}

    @Pointcut("execution(* com.lin.cloud.disk.service..*.*(..))")
    public void pointcut(){}

//    /**
//     * scserver层切点
//     */
//    @Pointcut("execution(* com.lin.blog2020..*.*(..))")
//    public void pointcut1(){}


    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        String className = null;
        String methodName = null;
        Object[] args = null;
        try {
            className = pjp.getTarget().getClass().getName();
            methodName = pjp.getSignature().getName();
            args = pjp.getArgs();
            log.info("################# 请求体类名：{},方法名：{},参数：{}", className, methodName, ArrayUtils.toString(args));

            Object proceed = pjp.proceed();
            log.info("################# 请求结果返回值：{}", JSON.toJSONString(proceed));
            return proceed;
        } catch (Throwable e) {
            // BusinessException自定义异常类不打印日志
            if (e instanceof BusinessException) {
                throw e;
            }
            // 在事务@Transaction下,抛出异常时已经回滚了,此时捕获的异常不属于事务内
            log.error("######### 类名：" + className + ",方法名：" + methodName + ",参数：" + ArrayUtils.toString(args) + ",异常信息：", e);
            throw e;
        }
    }

}
